﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Data.Model;
using NHibernate;
using NHibernate.Criterion;
using System.Collections.Generic;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.Weixin
{
    public partial class EditLBSShopInfo : System.Web.UI.Page
    {
        protected FrameHelper _frameHelper;

        protected WxLbsShopInfo _shopDb;

        private string _uid;

        protected string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            _frameHelper = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();
            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                Save();
            }
        }

        private void Save()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
              //  ITransaction tran = session.BeginTransaction();

                try
                {
                    if (string.IsNullOrEmpty(_uid))//新建
                    {
                        _shopDb = new WxLbsShopInfo();

                    }
                    _shopDb.ShopPhone = tb_phone.Text;
                    _shopDb.ShopName = tb_name.Text;
                    _shopDb.ShopAddress = tb_addr.Text;
                    _shopDb.BigPicUrl = fc_pic.Value;
                    _shopDb.SmallPicUrl = fc_spic.Value;
                    _shopDb.Longitude = decimal.Parse(UC_BaiduMap1.JingDu);
                    _shopDb.Latitude = decimal.Parse(UC_BaiduMap1.WeiDu);
                    _shopDb.Status = ddl_isa.SelectedValue;
                    _shopDb.Content = txtContent.Value;
                    _shopDb.IsNav = ddl_nav.SelectedValue;
                    _shopDb.Url = tb_url.Text;
                     _shopDb.ShopMan = tb_man.Text;
                     _shopDb.Province = ProvinceCascadeCombox1.SelectedValue;
                     _shopDb.City = CityCascadeCombox1.SelectedValue;
                     _shopDb.Area = AreaComboxControl1.SelectedValue;
                     _shopDb.OwnerAccount = "admin";

                     session.SaveOrUpdate(_shopDb);
                    session.Flush();
                 //   tran.Commit();
                }
                catch (System.Exception ex)
                {
               //     tran.Rollback();
                    _frameHelper.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }

            _frameHelper.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                navTabId = _frameHelper.NavTabId
            });
        }

        protected void InitializeGlobalValue()
        {
            if (!IsPostBack)
            {
                ParamKit.Bind("YesNo_10", ddl_isa, null, null, null, "-1", "-1");
                ParamKit.Bind("YesNo_10", ddl_nav, null, null, null, "-1", "-1");

                ViewState["uid"] = Request["uid"];
                if (string.IsNullOrEmpty(Request["uid"]))
                {
                    //设置图片默认值为上一条记录
                    using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                    {
                        WxLbsShopInfo lastShop = session.CreateQuery("from WxLbsShopInfo where OwnerAccount=:acc order by Id desc")
                            .SetParameter("acc", UserKit.GetUserAccountFailLogin())
                            .SetMaxResults(1)
                            .UniqueResult<WxLbsShopInfo>();
                        if (lastShop != null)
                        {
                            //fc_pic.Value = lastShop.BigPicUrl;
                            fc_spic.Value = lastShop.SmallPicUrl;
                            txtContent.Value = lastShop.Content;
                        }
                    }
                }
            }

            _uid = (string)ViewState["uid"];

            if (!string.IsNullOrEmpty(_uid))
            {
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    try
                    {
                        if (!IsPostBack)
                        {
                            ViewState["uid"] = Request["uid"];
                        }

                        _shopDb = session.CreateCriteria<WxLbsShopInfo>().Add(Expression.Eq("Id", int.Parse(_uid))).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxLbsShopInfo>();

                        if (_shopDb == null)
                        {
                            throw new Exception("找不到对应信息.");
                        }
                    }
                    catch (System.Exception ex)
                    {
                        _frameHelper.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                        return;
                    }
                }
            }
        }

        protected void InitializeContent()
        {
            //页面控件值
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(_uid))
                {
                    tb_name.Text = _shopDb.ShopName;
                    tb_addr.Text = _shopDb.ShopAddress;
                    tb_man.Text = _shopDb.ShopMan;
                    tb_phone.Text = _shopDb.ShopPhone;
                    tb_url.Text = _shopDb.Url;
                    fc_pic.Value = _shopDb.BigPicUrl;
                    fc_spic.Value = _shopDb.SmallPicUrl;
                    UC_BaiduMap1.JingDu = _shopDb.Longitude.ToString();
                    UC_BaiduMap1.WeiDu = _shopDb.Latitude.ToString();
                    if (string.IsNullOrEmpty(_shopDb.Status))
                    {
                        _shopDb.Status = "1";
                    }
                    if (string.IsNullOrEmpty(_shopDb.IsNav))
                    {
                        _shopDb.IsNav = "1";
                    }
                    ddl_isa.SelectedValue = _shopDb.Status;
                    ddl_nav.SelectedValue = _shopDb.IsNav;
                    txtContent.Value = _shopDb.Content;
                    ProvinceCascadeCombox1.SelectedValue = _shopDb.Province;
                    CityCascadeCombox1.SelectedValue=_shopDb.City;
                    AreaComboxControl1.SelectedValue = _shopDb.Area;
                }
                else
                {

                    ddl_isa.SelectedValue = "1";
                    ddl_nav.SelectedValue = "1";
                }
            }
        }
    }
}
